package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Brand;
import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.service.IBrandService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private SpuMapper spuMapper;

    public BrandServiceImpl(){
        log.debug("创建业务对象：BrandServiceImpl");
    }


    @Override
    public void addNew(BrandAddNewDTO brandAddNewDTO) {
        int count = brandMapper.countByName(brandAddNewDTO.getName());
        if (count>0){
            String message = "添加品牌失败!尝试添加的品牌名称已经被使用!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandAddNewDTO,brand);
        int rows = brandMapper.insert(brand);
        if (rows != 1){
            String message = "添加品牌失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除品牌】的业务，参数：{}",id);
        // 检查尝试删除的数据是否存在
        Object queryResult = brandMapper.getStandardById(id);
        if (queryResult == null){
            String message = "删除品牌失败，尝试删除的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        {
            int count = brandCategoryMapper.countByBrandId(id);
            if (count > 0) {
                String message = "删除品牌失败，存在“品牌与类别的关联数据”涉及此品牌！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }
        {
            int count = spuMapper.countByBrandId(id);
            if (count > 0) {
                String message = "删除品牌失败，存在SPU关联到此品牌！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
        }

        log.debug("即将开始删除数据，参数：{}",id);
        int rows = brandMapper.deleteById(id);
        if (rows != 1){
            String message = "删除品牌失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改品牌基本信息】的业务，参数ID：{}，新数据：{}", id, brandUpdateDTO);
        // 检查尝试修改的数据是否存在
        Object queryResult = brandMapper.getStandardById(id);
        if (queryResult == null){
            String message = "修改品牌失败，尝试修改的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //检查名称是否与其他的相册数据冲突
        int count = brandMapper.countByNameAndNotId(id, brandUpdateDTO.getName());
        if (count>0){
            String message = "修改品牌基本信息失败，品牌名称已被其他相册占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        Brand brand = new Brand();
        BeanUtils.copyProperties(brandUpdateDTO,brand);
        brand.setId(id);

        log.debug("即将修改数据：{}",brand);
        int rows = brandMapper.update(brand);
        if (rows != 1){
            String message = "修改品牌失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public void setEnable(Long id) {
        updateEnableById(id, 1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id, 0);
    }

    @Override
    public BrandStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询品牌详情】的业务，参数：{}", id);
        BrandStandardVO queryResult = brandMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询失败，尝试查询的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回品牌详情：{}", queryResult);
        return queryResult;
    }

    @Override
    public List<BrandListItemVO> list() {
        log.debug("开始处理【品牌列表】的业务，参数：无");
        return brandMapper.list();
    }

    private void updateEnableById(Long id, Integer enable){
        String[] enableText = {"禁用", "启用"};
        log.debug("开始处理【" + enableText[enable] + "品牌】的业务，品牌ID：{}，目标状态：{}", id, enable);
        BrandStandardVO brandStandardVO = brandMapper.getStandardById(id);
        if (brandStandardVO==null){
            String message = enableText[enable] + "品牌失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        if (brandStandardVO.getEnable().equals(enable)){
            String message = enableText[enable] + "品牌失败，当前品牌已经是"
                    + enableText[enable] + "状态！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Brand brand = new Brand();
        brand.setId(id);
        brand.setEnable(enable);
        log.debug("即将修改数据，参数：{}", brand);
        int rows = brandMapper.update(brand);
        if (rows != 1){
            String message = enableText[enable]+"品牌失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }
}
